﻿using CloudMusic.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CloudMusic.UC
{
    /// <summary>
    /// UCPaging.xaml 的交互逻辑
    /// </summary>
    public partial class UCPaging : UserControl, INotifyPropertyChanged
    {
        public UCPaging()
        {
            InitializeComponent();
            this.itemsNum.ItemsSource = _collection;
        }

        #region 事件
        /// <summary>
        /// 分页事件
        /// </summary>
        public event EventHandler<PageChangedEventArgs> PageChanged;
        #endregion

        #region 页码数字集合
        private ObservableCollection<PageItemModel> _collection = new ObservableCollection<PageItemModel>();
        private List<PageItemModel> _list = null;
        #endregion

        #region 分页相关属性
        private int _PageCount = 1;
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get { return _PageCount; }
            set
            {
                _PageCount = value;
                OnPropertyChanged("PageCount");
            }
        }

        private int _Page = 1;
        /// <summary>
        /// 当前页码
        /// </summary>
        public int Page
        {
            get { return _Page; }
            set
            {
                _Page = value;
                OnPropertyChanged("Page");

                CalcPageNumList(); //计算页码
            }
        }

        private long _RecordCount = 0;
        /// <summary>
        /// 记录总数
        /// </summary>
        public long RecordCount
        {
            get { return _RecordCount; }
            set
            {
                _RecordCount = value;
                OnPropertyChanged("RecordCount");
                CalcPageNumList(); //计算页码
            }
        }

        private int _PageSize = 10;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get { return _PageSize; }
            set
            {
                _PageSize = value;
                OnPropertyChanged("PageSize");

                CalcPageNumList(); //计算页码
            }
        }

        private int _ContinuousCount = 3;
        /// <summary>
        /// 当前页码右边连续页码数
        /// </summary>
        public int ContinuousCount
        {
            get { return _ContinuousCount; }
            set
            {
                _ContinuousCount = value;
                OnPropertyChanged("_ContinuousCount");
                CalcPageNumList(); //计算页码
            }
        }
        #endregion

        #region 单击页码事件
        private void btnNum_Click(object sender, RoutedEventArgs e)
        {
            if (PageChanged != null)
            {
                Button btn = sender as Button;
                PageItemModel itemModel = btn.CommandParameter as PageItemModel;
                if (itemModel.Page != Page)
                {
                    Page = itemModel.Page;
                    CalcPageNumList();
                    PageChangedEventArgs args = new PageChangedEventArgs(itemModel.Page);
                    PageChanged.Invoke(sender, args);
                }
            }
        }
        #endregion

        #region 计算页码
        /// <summary>
        /// 计算页码
        /// </summary>
        private void CalcPageNumList()
        {
            //long a1 = RecordCount / PageSize;
            //long b1 = RecordCount % PageSize;
            //if (b1 == 0)
            //{
            //    PageCount = Convert.ToInt32(a1);
            //}
            //else
            //{
            //    PageCount = Convert.ToInt32(a1 + 1);
            //}
            PageCount =Convert.ToInt32((RecordCount - 1) / PageSize + 1); //计算总页数PageCount

            _list = new List<PageItemModel>();

            //第一页
            PageItemModel item = new PageItemModel(1, Page);
            _list.Add(item);

            //当前页码连续页码
            for (int i = Page - ContinuousCount; i <= Page + ContinuousCount; i++)
            {
                if (i > 0 && i < PageCount)
                {
                    item = new PageItemModel(i, Page);
                    if (!_list.Exists(a => a.Page == item.Page))
                    {
                        _list.Add(item);
                    }
                }
            }

            //最后一页
            item = new PageItemModel(PageCount, Page);
            if (!_list.Exists(a => a.Page == item.Page))
            {
                _list.Add(item);
            }

            for (int i = _list.Count - 1; i > 0; i--)
            {
                if (_list[i].Page - _list[i - 1].Page > 1)
                {
                    _list.Insert(i, new PageItemModel(0, Page, 2));
                }
            }

            ////上一页下一页
            if (Page == 1)
            {
                this.prv.IsEnabled = false;
                this._prv.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CCCCCC"));
            }
            else
            {
                this.prv.IsEnabled = true;
                this._prv.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#8a8a8a"));
            }
            if (Page == PageCount)
            {
                this.next.IsEnabled = false;
                this._next.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CCCCCC"));
            }
            else
            {
                this.next.IsEnabled = true;
                this._next.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#8a8a8a"));
            }

            _collection.Clear();
            _list.ForEach(a => { _collection.Add(a); });
        }
        #endregion

        #region 上一页
        private void Prv_Click(object sender, RoutedEventArgs e)
        {
            int prePage = Page - 1;
            if (prePage < 1) prePage = 1;
            if (prePage != Page)
            {
                Page = prePage;
                CalcPageNumList();
                PageChangedEventArgs args = new PageChangedEventArgs(prePage);
                PageChanged.Invoke(sender, args);
            }
        }
        #endregion

        #region 下一页
        private void Next_Click(object sender, RoutedEventArgs e)
        {
            int nextPage = Page + 1;
            if (nextPage > PageCount) nextPage = PageCount;
            if (nextPage != Page)
            {
                Page = nextPage;
                CalcPageNumList();
                PageChangedEventArgs args = new PageChangedEventArgs(nextPage);
                PageChanged.Invoke(sender, args);
            }
        }
        #endregion

        #region INotifyPropertyChanged接口
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        #endregion

    }
    #region 分页事件参数
    /// <summary>
    /// 分页事件参数
    /// </summary>
    public class PageChangedEventArgs : EventArgs
    {
        private int _Page = 1;
        /// <summary>
        /// 当前页码
        /// </summary>
        public int Page
        {
            get
            {
                return _Page;
            }
        }

        /// <summary>
        /// 分页事件参数
        /// </summary>
        /// <param name="page">当前页码</param>
        public PageChangedEventArgs(int page)
        {
            _Page = page;
        }
    }
    #endregion
}
